package com.usian.utils.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.javafx.collections.MappingChange;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @创建人 lingyin
 * @创建时间 2022/10/14
 * @描述  实名认证接口工具类
 */
public class ApiFormIdUtils {

    private static final String IDCARDOCR = "f3675ec5b2e04bcc94b924580cf31efc";
    private static final String IDENTITYAPICODE = "6d2cd36fd997444c8b76cc555e570988";
    private static final String BIOLOGICAL  = "45eda52bbf24400290165418dc021d52";
    private static final String COMPARISONSOF  = "60958579f2374182ac4ec50deee5937a";


    public static Map<String,Object> IdcardOCR(Map map){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode",IDCARDOCR);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON); // json
//        Map<String,String> map = new HashMap<>();
//        map.put("image",imageurl);
//        map.put("imageType","URL");
//        map.put("ocrType","0");
        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(map), httpHeaders); // 封装请求参数
        ResponseEntity<String> response = restTemplate.postForEntity("https://api.yonyoucloud.com/apis/dst/IdcardOCR/IdcardOCR", formEntry, String.class);// 发送一个post请求
        // 只要分数
        Map<String, Object> requestBody = JSON.parseObject(response.getBody());
        Map<String, Object> data = (Map<String, Object>)requestBody.get("data");
        data.put("code",requestBody.get("code"));
        return data;
    }

    /**
     * params.put("idNumber", "?"); 身份证号
     * params.put("userName", "?"); 身份证姓名
     * @return
     */
    public static String matchIdentity(Map<String,String> map){
        // 路径  头信息   请求体   请求方式   返回值
        RestTemplate restTemplate = new RestTemplate();
        //头信息
        HttpHeaders httpHeaders = new HttpHeaders();
        // 内容类型  编码格式
        httpHeaders.add("apicode",IDENTITYAPICODE);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
//         设置请求参数
        Map<String,String> params = new HashMap<>();
        params.put("idNumber", map.get("cardNum"));
        params.put("userName", map.get("name"));
//        设置请求体
        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(params), httpHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity("https://api.yonyoucloud.com/apis/dst/matchIdentity/matchIdentity",formEntry,String.class);
        Map<String, Object> requestBody = JSON.parseObject(response.getBody());
        String code= (String) requestBody.get("code");

        return code;
    }
    /**
     * 活体照片
     * @return
     */
    public static String bioLogical (String liveImage){
        // 路径  头信息   请求体   请求方式   返回值
        RestTemplate restTemplate = new RestTemplate();
        //头信息
        HttpHeaders httpHeaders = new HttpHeaders();
        // 内容类型  编码格式
        httpHeaders.add("apicode",BIOLOGICAL);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
//         设置请求参数
        Map<String,String> params = new HashMap<>();
        params.put("image", liveImage);
        params.put("imageType","URL");
//        设置请求体
        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(params), httpHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity("https://api.yonyoucloud.com/apis/dst/Biologicalexamination/Biologicalexamination",formEntry,String.class);
        // 只要分数
        Map<String, Object> requestBody = JSON.parseObject(response.getBody());
        Map<String, Object> data = (Map<String, Object>)requestBody.get("data");
        String score = (String) data.get("score");
        return score;
    }
        //
    /**
     *人证核验 需要 map.put("idCardImage",身份证正面照);
     *       map.put("imageType","URL");
     *       map.put("liveImage",活体照片);
     * @return
     */
    public static String comparisonsofPersonsandDocuments(Map map){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode",COMPARISONSOF);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON); // json
//        Map<String,String> map = new HashMap<>();
//        map.put("image",imageurl);
//        map.put("imageType","URL");
//        map.put("ocrType","0");
        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(map), httpHeaders); // 封装请求参数
        ResponseEntity<String> response = restTemplate.postForEntity("https://api.yonyoucloud.com/apis/dst/ComparisonsofPersonsandDocuments/ComparisonsofPersonsandDocuments", formEntry, String.class);// 发送一个post请求
        // 只要分数
        Map<String, Object> requestBody = JSON.parseObject(response.getBody());
        Map<String, Object> data = (Map<String, Object>)requestBody.get("data");
         String score = (String) data.get("score");
        return score;
    }

}
